﻿package at.geoathome.utils.font
{
	import com.kronomy.utils.DevUtils;
	import flash.display.Loader;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFormat;

	/**
	 * ...
	 * @author Georg Kamptner & Thomas Klana
	 */
	public class EmbedFont extends EventDispatcher
	{
		
		//
		// VARIABLES:
		private static var _instance:EmbedFont = null;	
		
		private var _loader:Loader;
		private var _font_name:String;
		private var _source_folder_url:String;
		
		
		public function EmbedFont(font_name:String, source_folder_url:String, auto_load:Boolean = false) 
		{
			_font_name = font_name;
			_source_folder_url = source_folder_url;
			
			if (auto_load)
				load(font_name, source_folder_url);
		}
		

		// FUNCTIONS & METHODS:
		
		public function load(font_name:String, source_folder_url:String):void
		{	
			_font_name = font_name;
			_source_folder_url = source_folder_url;
			
			// load Font SWF
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete );
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);			
			var fontURL:URLRequest = new URLRequest( _source_folder_url + font_name + ".swf" );
			_loader.load(fontURL);				
		}
		
		private function onIOError(e:IOErrorEvent):void
		{
			// dispatch complete
			dispatchEvent( new EmbedFontEvent(_font_name, Event.COMPLETE) );
			
			throw new IOError("Font load failed.");
		}
		
		private function onLoadComplete(event:Event):void
		{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadComplete );
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
			
			// register
			var font_class:Class = Loader(event.target.loader).contentLoaderInfo.applicationDomain.getDefinition( _font_name ) as Class;
			register(font_class);
			
			_loader = null;
        }
		
		private function register(font_class:Class):void
		{
			// register font
			Font.registerFont(font_class);
			
			// dispatch complete
			dispatchEvent( new EmbedFontEvent(_font_name, Event.COMPLETE) );
        }
		
		/*
		private function setTextFieldFont( fontName:String, textfield:TextField, format:TextFormat ):void
		{
			EmbedFontLoader.loadAndRegister( fontName );
			var instance:EmbedFontLoader = EmbedFontLoader.getInstance();
			instance.addEventListener( Event.COMPLETE, function(e:EmbedFontEvent) {	
				// break condition: another font was loaded
				if(e.fontName != fontName) return;
				
				var font:Font = new ( instance.getFont( fontName ) )() as Font;
				format.font = font.fontName;
				textfield.embedFonts = true;
				textfield.setTextFormat(format);
			});
		}
		
		
		// - - - - - - - - - - - - - - - - - - - 
		//
		
		//
		// PUBLIC STATIC INTERFACE:
		public static function setTextFieldFont( fontName:String, textfield:TextField, format:TextFormat ) :void
		{
			EmbedFont.getInstance().setTextFieldFont(fontName, textfield, format);
		}
		// - - - - - - - - - - - - - - - - - - - 
		//
		*/
	}
}
